LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 06-03-2010, 09:34 PM   #1
anon021
LQ Newbie
 
Registered: Jun 2010
Posts: 2

Rep: Reputation: 0
Traversing/Counting contents of /proc/[pid]/fd directories


Hello,

I posted this on another forum, and I figure I would try here as well.

I have a rather straightforward challenge and I need a little help. Let me preface that I am not a very experienced kernel module developer, but I have read through some of the basic tutorials and examples. I also have a fair amount of C/C++ experience, so I am not a complete novice in those regards.

The challenge is to count the contents of each /proc/[pid]/fd directory from a kernel module. Basically, counting the number of open file descriptors for each process. Now, I can get each pid using the following segment:

Code:
struct task_struct *task;
for_each_process(task)
{
    task->pid
}
From that, I can construct the absolute pathname /proc/[pid]/fd for each process, and use the path_loookup() method to begin further analysis of the directory. After that, I am struggling on how to traverse/count the contents of the /fd directories. I can get the struct dentry object for the directory using struct dentry *lookup_create(), but I don't have a good enough understanding of using the struct dentry or struct inode types to proceed after that.

Any suggestions on how to properly proceed or recommendations on how I can accomplish this task. I know there are easy ways to do this from user space; however, that option is not available. The challenge is to do it from kernel space.

I am developing this for kernel distro 2.6.31-14-generic on Ubuntu 9.10 Karmic Koala.

Thank you for any help.

*************************************
Updated later with the follow-up post
*************************************
I have been doing some more looking around and have yet to figure out anything definitive.

However, I did discover the functions proc_root_readdir() and others located in linux/fs/proc/root.c. Is this headed in the right direction or am I heading down the wrong "Rabbit Hole"?

Another thought based on other examples and calls I have seen: should I try to get the "struct file" object for a /proc/[pid]/fd directory and then try to count the file offsets from the directory (ignoring the offsets 1 and 2 for "." and ".." respectively)?

Once again, I apologize if I am way off base. If so, please provide some feedback to put me down the proper path.
 
Old 06-08-2010, 08:45 PM   #2
ggeagle
LQ Newbie
 
Registered: Jun 2008
Location: DaeJeon,Korea
Posts: 5

Rep: Reputation: 0
Because of my poor english,this may out of some misunderstanding....

I am not a programmer either linux user,further have no computer...this is my smartphone;;;

include/linux/file.h
Per-Process file table ->fdtable
Open file table -> files_struct

So,The 'fdtable' struct may have key possiblity to solve this problem

This is my first posting to LQ which requires at least one posting to access full user account...
This policy force to use english all over the world...may not a quite wide open concepts

below detail description and blog link is just for Korean Alphabet (called Han Gl)

bye.

위 커널소스에서 fdtable 구조체가 하나의 프로세서와 링크된 파일 디스크립터 테이블 이다.아마도 이 질문자는 /proc 디렉토리에 집착하고 있는데 이는 디스크의 실제 파일이 아니라 시스템 메모리 트리구조에 그대로 대응되는 것이므로 이 디렉토리의 각종 값을 만지면 메모리를 임의로 만지는 것이 된다.

물론 on/off 플래그나 16비트 상수를 cat >> 명령 등으로 쓰는 정도는 자주 이용되지만 fd 를 만지면 실제 파일 컨텐츠를 포함한 메모리블럭은 링크가 깨진 object 가 된다.
아마도 일정 시간이 지나면 이런 메모리블럭이 리플레쉬 되는 루틴이 있겠지만 나는 알지 못한다.

다음은 파일디스크립터와 관련된 내 멋대로의 글이다.
오류에 대해서는 책임지지 않는다.

http://blog.naver.com/ggeagle?Redire...logNo=97863037

Last edited by ggeagle; 06-10-2010 at 07:20 AM.
 
Old 06-12-2010, 08:41 AM   #3
anon021
LQ Newbie
 
Registered: Jun 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Ok,

It has taken me awhile to get around to posting this, but I did find the solution to my problem about a week ago. It looks like I was heading down the right path by looking at the file descriptor table. Below is a segment of code that will look through the file descriptor table for each process (PID) and identify each open file descriptor.

Code:
struct task_struct* ptask;
struct fdtable* pfdt;

for_each_process(ptask) 
{
    rcu_read_lock();	
    pfdt = files_fdtable(ptask->files);

    for (int n = 0; n < pfdt->max_fds; n++)
    {
        if (NULL != pfdt->fd[n])
	{
	    // pfdt->fd[n] is an open file descriptor if it is not NULL,
            // and n (the index) corresponds to the file descriptor
            // number you will see in /proc/[pid]/fd
	}            
    }
    rcu_read_unlock();
}
Good luck to anyone with related problems, and thank you for your comments and suggestions.
 
  


Reply

Tags
descriptor, descriptors, file, filesystem, kernel, module, proc



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pmap or /proc/<pid>smap or /proc/<pid>/status iQoder Linux - Newbie 1 07-16-2009 06:32 PM
/proc/pid/maps Moraxus Linux - General 0 02-11-2009 08:49 AM
/proc/pid/io seems not to work kornelix Linux - Server 5 06-17-2007 02:39 AM
hidden directories under proc... what are they for? : (/proc/.23142) syssyphus Linux - General 1 04-10-2006 03:23 PM
Recursively traversing sub-directories ark86 Linux - Newbie 2 01-29-2006 01:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 05:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration